package com.example.xpvideo.service.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.example.xpvideo.context.PageContext;
import com.example.xpvideo.entity.*;
import com.example.xpvideo.entity.req.SupplierMerchantReq;
import com.example.xpvideo.entity.req.TopLiveReq;
import com.example.xpvideo.entity.req.UpdateReq;
import com.example.xpvideo.entity.res.CommodityApplyRes;
import com.example.xpvideo.entity.res.MerchantsDetailRes;
import com.example.xpvideo.entity.res.MerchantsRes;
import com.example.xpvideo.enums.ErrCodeEnum;
import com.example.xpvideo.exception.ErrorException;
import com.example.xpvideo.mapper.LiveBroadcastMapper;
import com.example.xpvideo.mapper.MerchantsConfigMapper;
import com.example.xpvideo.mapper.MerchantsMapper;
import com.example.xpvideo.mapper.PurchaseUsersMapper;
import com.example.xpvideo.service.SupplierMerchantsService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.List;

/**
 * @author : 洪俊新
 * @date : 2021/7/9 14:16
 * @description:
 */
@Service
public class SupplierMerchantServiceImpl implements SupplierMerchantsService {
    @Autowired
    private MerchantsMapper merchantsMapper;
    @Autowired
    private MerchantsConfigMapper merchantsConfigMapper;
    @Autowired
    private LiveBroadcastMapper liveBroadcastMapper;



    @Override
    public ResponseResult<MerchantsDetailRes> detail(String account) throws ErrorException {

        MerchantsDetailRes merchantsDetailRes = merchantsMapper.merchantsDetail(Wrappers.<Merchants>query()
                        .eq("account",account));

        return ResponseResult.responseResult(0, merchantsDetailRes);

    }

    @Override
    public ResponseResult<Object> updateMStage(UpdateReq updateReq) throws ErrorException{
        Integer id = updateReq.getId();
        Integer stage = updateReq.getStage();

        MerchantsConfig merchantsConfig = new MerchantsConfig();
        merchantsConfig.setStage(String.valueOf(stage));
        int res = merchantsConfigMapper.update(merchantsConfig,Wrappers.<MerchantsConfig>update().eq("merchants_id",id));

        if(res > 0){
            return ResponseResult.responseResult(0,"冻结/解冻商户成功！");
        }
        throw new ErrorException(ErrCodeEnum.unknown_20000);
    }

    @Override
    public ResponseResult<Object> topLive(TopLiveReq topLiveReq) throws ErrorException {
        Integer id = topLiveReq.getId();
        Integer stage = topLiveReq.getStage();
        Integer topDays = topLiveReq.getTopDays();

        LiveBroadcast liveBroadcast = new LiveBroadcast();
        liveBroadcast.setIsTop(stage);



        int res = liveBroadcastMapper.update(liveBroadcast, Wrappers.<LiveBroadcast>update()
                .set(stage == 1, "top_days", topDays)
                .eq("merchants_id", id));

        if (res > 0) {
            return ResponseResult.responseResult(0, "置顶/取消置顶商户直播间成功！");
        }

        throw new ErrorException(ErrCodeEnum.unknown_20000);

    }


    @Override
    public ResponseResult<Object> recommendLive(UpdateReq updateReq) throws ErrorException{
        Integer id = updateReq.getId();
        Integer stage = updateReq.getStage();

        LiveBroadcast liveBroadcast = new LiveBroadcast();
        liveBroadcast.setIsRecommend(stage);
        int res = liveBroadcastMapper.update(liveBroadcast,Wrappers.<LiveBroadcast>update().eq("merchants_id",id));

        if(res > 0){
            return ResponseResult.responseResult(0,"推荐/取消推荐商户直播间成功！");
        }
        throw new ErrorException(ErrCodeEnum.unknown_20000);
    }

    @Override
    public ResponseResult<Object> isOffcial(UpdateReq updateReq) throws ErrorException{
        Integer id = updateReq.getId();
        Integer stage = updateReq.getStage();

        MerchantsConfig merchantsConfig = new MerchantsConfig();
        merchantsConfig.setIsOfficial(String.valueOf(stage));
        int res = merchantsConfigMapper.update(merchantsConfig,Wrappers.<MerchantsConfig>update().eq("merchants_id",id));

        if(res > 0){
            return ResponseResult.responseResult(0,"设为官方号/取消官方号成功！");
        }
        throw new ErrorException(ErrCodeEnum.unknown_20000);
    }
}
